package org.example.function;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class Java8Function3 {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("node", "c++", "java", "javascript");
        Map<String,Integer> map = convertListToMap(list, s -> s.length());
        System.err.println(JSONObject.toJSONString(map));
        Map<String,Integer> map1 = convertListToMap(list,x -> getLength(x));
        System.err.println(JSONObject.toJSONString(map1));
    }

    static <T,R> Map<T,R> convertListToMap(List<T> list, Function<T,R> func){
        Map<T,R> map = new HashMap<>();
        for (T t : list) {
            map.put(t, func.apply(t));
        }
        return map;
    }
    static int getLength(String str){
        return str.length();
    }
}
